# -*- coding: utf-8 -*-

from rest_framework.decorators import action
from core.viewset import CustomModelViewSet
from core.response import JsonResponse
from apps.sys.models import Menu
from apps.sys.menu.serializers import *
from rest_framework.response import Response
from utils.tree import list2tree
from .filters import *


class MenuViewSet(CustomModelViewSet):
    """
    菜单管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """

    queryset = Menu.objects.all()
    serializer_class = MenuSerializer
    create_serializer_class = MenuCreateSerializer
    update_serializer_class = MenuCreateSerializer
    filterset_class = MenuFilterSet
    extra_filter_backends = []

    @action(detail=False, methods=["GET"])
    def treeselect(self, request, *args, **kwargs):
        queryset = Menu.objects.all()
        serializer = MenuTreeSelectSerializer(queryset, many=True, request=request)
        data = list2tree(serializer.data)
        return JsonResponse(data=data, msg="获取成功")
